﻿#region 头注释
/******************************************************************************
文件名称    :   ChemistryPg.cs
创建日期    :   2012/1/3 20:56:21
功能描述    :   前列腺液检查实体类
修改日志    :   
    2012-01-03 20:56  马利
*******************************************************************************/
#endregion

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Newtonsoft.Json;
using Dare.Utilities.Data;

namespace Stone.Components.Entities
{
    /// <summary>
    /// 前列腺液检实体类
    /// </summary>
    public class ChemistryPG : EntityBase<ChemistryPG, long>
    {
        #region 构造函数

        /// <summary>
        /// 默认构造函数
        /// </summary>
        public ChemistryPG()
            : base()
        {
            base.xmlSerializer = new DefaultXmlSerializer(this);
            base.jsonSerializer = new DefaultJsonSerializer(this);
        }

        public override void Reset()
        {
            lecithin = null;
            rbc = null;
            wbc = null;
            comment = null;
            timeStamp = DateTime.MinValue;
        }

        public override ChemistryPG CreateNew()
        {
            return new ChemistryPG();
        }

        #endregion

        #region 属性

        /// <summary>
        /// 获取实体类型
        /// </summary>
        public override EntityType EntityType
        {
            get { return EntityType.ChemistryPG; }
        }

        /// <summary>
        /// 获取或设置卵磷脂小体
        /// </summary>
        public string Lecithin
        {
            get { return lecithin; }
            set
            {
                if (entityState == EntityState.Saved && lecithin != value) entityState = EntityState.Changed;
                lecithin = value;
            }
        }
        protected string lecithin;

        /// <summary>
        /// 获取或设置红细胞
        /// </summary>
        public string Rbc
        {
            get { return rbc; }
            set
            {
                if (entityState == EntityState.Saved && rbc != value) entityState = EntityState.Changed;
                rbc = value;
            }
        }
        protected string rbc;

        /// <summary>
        /// 获取或设置白细胞
        /// </summary>
        public string Wbc
        {
            get { return wbc; }
            set
            {
                if (entityState == EntityState.Saved && wbc != value) entityState = EntityState.Changed;
                wbc = value;
            }
        }
        protected string wbc;

        /// <summary>
        /// 获取或设置Comment
        /// </summary>
        public string Comment
        {
            get { return comment; }
            set
            {
                if (entityState == EntityState.Saved && comment != value) entityState = EntityState.Changed;
                comment = value;
            }
        }
        protected string comment;

        /// <summary>
        /// 获取或设置TimeStamp
        /// </summary>
        public DateTime TimeStamp
        {
            get { return timeStamp; }
            set
            {
                if (entityState == EntityState.Saved && timeStamp != value) entityState = EntityState.Changed;
                timeStamp = value;
            }
        }
        protected DateTime timeStamp;

        #endregion

        #region 方法

        #endregion

        #region 实体复制相关方法

        /// <summary>
        /// 复制当前实体属性到指定实体上
        /// </summary>
        /// <param name="entity">要把属性复制到的实体</param>
        /// <returns>返回传入的实体</returns>
        public override ChemistryPG CopyTo(ChemistryPG entity)
        {
            if (base.CopyTo(entity) == null) return null;

            entity.lecithin = this.lecithin;
            entity.rbc = this.rbc;
            entity.wbc = this.wbc;
            entity.comment = this.comment;
            entity.timeStamp = this.timeStamp;

            return entity;
        }

        #endregion

        #region 默认XML序列器类

        public class DefaultXmlSerializer : XmlSerializerBase, IEntityXmlSerializer<ChemistryPG>
        {
            ChemistryPG entity;
            public DefaultXmlSerializer(ChemistryPG entity)
                : base(entity.GetType().Name)
            {
                this.entity = entity;
            }

            public ChemistryPG Entity
            {
                get { return entity; }
                set { entity = value; }
            }

            protected override void BeforeReadXml()
            {
                entity.Reset();
            }

            /// <summary>
            /// 读取XML属性
            /// </summary>
            /// <param name="name">属性名</param>
            /// <param name="value">属性值</param>
            /// <param name="reader">XML读取器</param>
            internal override void ReadXmlAttribute(string name, string value, XmlReader reader)
            {
                switch (name)
                {
                    case "id":
                        entity.id = XmlConvert.ToInt64(value);
                        break;
                    case "timeStamp":
                        entity.timeStamp = new DateTime(long.Parse(value));
                        break;
                    case "entityState":
                        entity.entityState = (EntityState)Enum.Parse(typeof(EntityState), value, true);
                        break;
                }
            }

            /// <summary>
            /// 读取XML子节点
            /// </summary>
            /// <param name="name">节点名</param>
            /// <param name="reader">XML读取器</param>
            internal override void ReadXmlChildElement(string name, XmlReader reader)
            {
                switch (name)
                {
                    case "Lecithin":
                        entity.lecithin = reader.ReadElementString();
                        break;
                    case "Rbc":
                        entity.rbc = reader.ReadElementString();
                        break;
                    case "Wbc":
                        entity.wbc = reader.ReadElementString();
                        break;
                    case "Comment":
                        entity.comment = reader.ReadElementString();
                        break;
                }
            }

            /// <summary>
            /// 写入XML属性
            /// </summary>
            /// <param name="writer">XML编写器</param>
            internal override void WriteXmlAttributes(XmlWriter writer)
            {
                writer.WriteAttributeString("id", XmlConvert.ToString(entity.Id));
                writer.WriteAttributeString("timeStamp", entity.TimeStamp.Ticks.ToString());
                writer.WriteAttributeString("entityState", entity.EntityState.ToString());
            }

            /// <summary>
            /// 写入XML子节点
            /// </summary>
            /// <param name="writer">XML编写器</param>
            internal override void WriteXmlChildElements(XmlWriter writer)
            {
                if (entity.Lecithin != null) writer.WriteElementString("Lecithin", entity.Lecithin);
                if (entity.Rbc != null) writer.WriteElementString("Rbc", entity.Rbc);
                if (entity.Wbc != null) writer.WriteElementString("Wbc", entity.Wbc);
                if (entity.Comment != null) writer.WriteElementString("Comment", entity.Comment);
            }
        }

        #endregion

        #region 默认Json序列化器类

        public class DefaultJsonSerializer : JsonSerializerBase, IEntityJsonSerializer<ChemistryPG>
        {
            ChemistryPG entity;
            public DefaultJsonSerializer(ChemistryPG entity)
                : base()
            {
                this.entity = entity;
            }

            public ChemistryPG Entity
            {
                get { return entity; }
                set { entity = value; }
            }

            /// <summary>
            /// 读取JSON属性
            /// </summary>
            /// <param name="propName">属性名称</param>
            /// <param name="propValue">属性值</param>
            /// <param name="reader">JSON读取器</param>
            protected override void ReadJsonProperty(string propName, object propValue, JsonReader reader)
            {
                switch (propName)
                {
                    case "id":
                        entity.id = DbConvert.ToNullableInt64(propValue) ?? entity.id;
                        break;
                    case "lecithin":
                        entity.lecithin = Convert.ToString(propValue) ?? entity.lecithin;
                        break;
                    case "rbc":
                        entity.rbc = Convert.ToString(propValue) ?? entity.rbc;
                        break;
                    case "wbc":
                        entity.wbc = Convert.ToString(propValue) ?? entity.wbc;
                        break;
                    case "comment":
                        entity.comment = Convert.ToString(propValue) ?? entity.comment;
                        break;
                    case "timeStamp":
                        entity.timeStamp = new DateTime(DbConvert.ToNullableInt64(propValue) ?? 0);
                        break;
                }
            }

            /// <summary>
            /// 写入JSON属性
            /// </summary>
            /// <param name="writer">JSON编写器</param>
            protected override void WriteJsonProperties(JsonWriter writer)
            {

                //Id
                writer.WritePropertyName("id");
                writer.WriteValue(entity.Id);

                //卵磷脂小体
                writer.WritePropertyName("lecithin");
                if (entity.Lecithin == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.Lecithin);
                }

                //红细胞
                writer.WritePropertyName("rbc");
                if (entity.Rbc == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.Rbc);
                }

                //白细胞
                writer.WritePropertyName("wbc");
                if (entity.Wbc == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.Wbc);
                }

                //Comment
                writer.WritePropertyName("comment");
                if (entity.Comment == null)
                {
                    writer.WriteNull();
                }
                else
                {
                    writer.WriteValue(entity.Comment);
                }

                //TimeStamp
                writer.WritePropertyName("timeStamp");
                writer.WriteValue(entity.TimeStamp.Ticks);
            }
        }


        #endregion
    }
}